//判断是否微信
   var wechat=false;
   var ua = navigator.userAgent.toLowerCase();
    if(ua.match(/MicroMessenger/i)=="micromessenger") {
	   wechat=true;	
    } 
	wechat=true;
//用户选择时间

	  $("#time").change(function(){	  
		  selectSpan();
		    var time=$('#time option:selected').val(); 
			$(".timeSpan").html(time);
	
	  })
	  $("#clock").change(function(){
		 var clock=$('#clock option:selected').val(); 
	    $(".clockSpan").html(clock);
	  })

//初始化未来30天的日期，并加入到select中
     var Week = ['日','一','二','三','四','五','六'];
     function getNextDay(i){
        var d = new Date();
        d = +d + 1000*60*60*24*i;
        d = new Date(d);
		var c=d.getDay();
		var y=d.getFullYear();

		var a=String(d.getMonth()+1)
		if (a.length==1){
			a="0"+a;
			}
		var b=String(d.getDate());
		if(b.length==1){
			b="0"+b;
			}
		return (y+"年"+a+"月"+b+"日"+" 星期"+Week[c]); 
        }
     for(var i=0;i<30;++i){
         var node1 = document.createElement("option");
		 node1.innerHTML=getNextDay(i);
		 document.getElementById("time").appendChild(node1)	
	    } 
	  $("#province").change(function(){
		 selectSpan();
	  })
	  $("#city").change(function(){
		 selectSpan();
	  })
	  //地区选择
	  $("#addressSelect").show();
	  $("#contactAddress").attr("placeholder","请填写详细地址(区域街道门牌号等)")
	  
	  
	    
//初始化选择
function selectSpan(){
		var province=$('#s_province option:selected').val(); 
	    $(".provinceSpan").html(province);
		var city=$('#s_city option:selected').val(); 
	    $(".citySpan").html(city);
		var county=$('#s_county option:selected').val(); 
	    $(".areaSpan").html(county);
	  };
	  
//用户增加或者减少用餐人数的操作

       $("#reduceButton").bind("touchstart",function(e){ 
	       $(this).find("img").attr("src",styleUrl+"/styles/img/reduceIconHover.png");  
	       var peopleNum=Number($(this).siblings(".buyNumber").val()); 
			if(peopleNum==Number(initData.orderPerson)){
		     commonTckBlack("人数不能少于"+initData.orderPerson+"人");
			 $(this).siblings(".buyNumber").val(initData.orderPerson);
			 return;
		    }
			peopleNum=Number(peopleNum)-1;
			$(this).siblings(".buyNumber").val(peopleNum);
			$("#orderPerson").html(peopleNum)
			totalPrice();
		  },false)
	   $("#reduceButton").bind("touchend",function(e){
		    $(this).find("img").attr("src",styleUrl+"/styles/img/reduceIcon.png"); 
		   })
	   $("#addButton").bind("touchstart",function(e){ 
	       $(this).find("img").attr("src",styleUrl+"/styles/img/addIconHover.png");

	       var peopleNum=Number($(this).siblings(".buyNumber").val()); 
		
			peopleNum=Number(peopleNum)+1;
			$(this).siblings(".buyNumber").val(peopleNum);
			$("#orderPerson").html(peopleNum)
			totalPrice();
		  },false)
	    $("#addButton").bind("touchend",function(e){
		   $(this).find("img").attr("src",styleUrl+"/styles/img/addIcon.png"); 
		   })
	   $(".buyNumber").change(function(){
		   var peopleNum=Number($(this).val());
           if(isNaN(peopleNum)){
			  commonTckBlack("请输入数字");
			   $(this).val(1); 
		   }		   
		   if(peopleNum<Number(initData.orderPerson)){
			   commonTckBlack("人数不能少于"+initData.orderPerson+"人");
			   $(this).val(initData.orderPerson);
			   }
		   totalPrice();
		})
//地址备注弹出框管理按钮
//地址发票弹出框管理按钮
 $(".remark").click(function(){
      $(".remarkTck").fadeIn();
	  $(".applySelectBg").fadeIn();
   })
//发票完成赋值
  $("#remarkSubmit").click(function(){
	  var remarkDetail=$(".remarkInput").val();
	  if(remarkDetail!=""){
	  $("#remarkCon").html(remarkDetail);}
	  $(".remarkTck").fadeOut();
	$(".applySelectBg").fadeOut();
	  })


   $("#addressButton").click(function(){
      $(".addressTck").fadeIn();
	  $(".applySelectBg").fadeIn();
   })
   //地址完成赋值
  $("#addressSubmit").click(function(){
    var name=$("#contactName").val();
	var tel=$("#contactTel").val();
	var address=$("#contactAddress").val();
	if(name!=""||tel!=""||address!=""){
	   $("#applyName").html(name);
	   $("#applyTel").html(tel);
	   $("#applyAddress").html(address)
	//周末牛排限时抢特殊规则

		   var showProvince=$("#s_province option:selected").val();
		   var showCity=$("#s_city option:selected").val();
		   var showCounty=$("#s_county option:selected").val();
		   
		 if(showProvince=="省份")showProvince="";
		 if(showCity=="地级市")showCity="";
		 if(showCounty=="市、县级市")showCounty="";
		 //将填写的地址保存在本地
		 var addressInfo={
			 "name":name,
			 "tel":tel,
			 "province":showProvince,
			 "city":showCity,
			 "county":showCounty,
			 "address":address
			 }
		 localStorage.addressInfo=JSON.stringify(addressInfo);
		 if(showProvince==showCity){
			showCity="";
			 }
		
	    $("#applyAddress").html(showProvince+showCity+showCounty+address)
	
	   $(".addressLiDetail").show();
	   $("#addressHint").hide();
	 }
	 else{
	   $("#applyName").html(name);
	   $("#applyTel").html(tel);
	   $("#applyAddress").html(address);
	   $(".addressLiDetail").hide();
	   $("#addressHint").show();
	 }
	$(".addressTck").fadeOut();
	$(".applySelectBg").fadeOut();
  })

   $(".applySelectBg").click(function(){
      $(".remarkTck").fadeOut();
	  $(".addressTck").fadeOut();
	  $(".applySelectBg").fadeOut();
   })

//倒计时
	var second;
	var secondReduce;
	function countDown(){
		$(".countDown").show();
		second=60;
	    secondReduce=setInterval("secondDown()",1000);
		}	
	function secondDown(){
		    second=second-1;
			$(".countDown").html(second+"秒");
			if(second==0){
				$(".countDown").html("60秒").hide();
				$("#getCode").html("重新获取");
				clearInterval(secondReduce);
				}
			}	
		
//发送验证码
    if(wechat==true){
		document.getElementById("getCode").addEventListener("click",function(e){
		checkTel(function(tel){//检查手机号填写
		      if($("#contactTel").val()==""&&$("#applyTel").html()==""){
				  $("#contactTel").val(tel); 
			      $("#applyTel").html(tel);
				  }	      
			  $.post(initData.getCodeApi,{"phoneNo":tel,"deviceId":"w_"+initData.userOpenId},function(){
				  countDown();
				  })
			},false)})
	}
//语音验证码
    $(".voiceButton a").click(function(e) {
		$(".applySelectBg").fadeIn();
        $(".videoLog").fadeIn();
    });
	$(".default").click(function(e) {
        $(".applySelectBg").fadeOut();
        $(".videoLog").fadeOut();
    });
	
	$(".primary").click(function(e) {
		$(".applySelectBg").fadeOut();
        $(".videoLog").fadeOut();
        checkTel(function(tel){//检查手机号填写
		      if($("#contactTel").val()==""&&$("#applyTel").html()==""){
				  $("#contactTel").val(tel); 
			      $("#applyTel").html(tel);
				  }	      
			  $.post(initData.getVoiceCodeApi,{"phoneNo":tel,"deviceId":"w_"+initData.userOpenId},function(data){
				 if(Number(data.success)==1){
					 commonTckBlack("发送成功！请留意您的来电。");
					 }
				  else{
					  commonTckBlack(data.errorMessage);
					  }
				  })
			},false)
    });	 
//计算总价
function totalPrice(){
//价格变动，总价跳动动画开始
		 $(".allPriceLi").stop();
		 $(".allPriceLi #allPriceNum").stop();
		 $(".allPriceLi").animate({height:"0px",overflow:"hidden"},50);
		 $(".allPriceLi #allPriceNum").animate({marginTop:"-15px",opacity:"0"},50);
		 setTimeout(function(){
		 $(".allPriceLi").animate({height:"52px",overflow:"auto"});
		 $(".allPriceLi #allPriceNum").animate({marginTop:"0px",opacity:"1"});
		 },50); 
		//价格变动，总价跳动动画结束 
	   var menuPrice=Number($("#menuPrice").html());
	   var orderPerson=Number($("#orderPerson").html());	
	   var payedPrice=initData.cardPrice;
	   var totalPrice=menuPrice*orderPerson-payedPrice;
	   if(totalPrice<0)totalPrice=0;
	   $("#totalPrice").html(totalPrice);
	   $(".lastPrice b").html(totalPrice);
	}
totalPrice()	
	
//验证电话号码方法
  function checkTel(callback){ 
	  var tel=$("#tel").val();	
        var partten = /^1[3,5,7,8]\d{9}$/;
          if(partten.test(tel))
          { }
		else
          {
     commonTckBlack("请填写正确的手机号码");
	   return;
          } 
	   callback(tel);
		  }
		  
$("#submit").click(function(){

  //验证是否购买
 
//如果是微信，获取姓名电话号码地址
   var tel1=$("#tel").val();
   var tel2=$("#applyTel").html();
   var name=$("#applyName").html();
   if(!tel1){tel1=tel2};
   var address=$("#applyAddress").html();
   var address2=$("#contactAddress").val();
   var code=$("#code").val();
   var partten = /^1[3,5,7,8]\d{9}$/;
   if(partten.test(tel1))
          { }
		else
          {
     commonTckBlack("请填写正确的手机号码");
	   return;
          }  
   if(code.length!=6||isNaN(code)==true){
	  commonTckBlack("请填写正确的验证码");
	   return;
	 }
	if($("#s_province option:selected").val()=="省份"||$("#s_city option:selected").val()=="地级市"||$("#s_county option:selected").val()=="市、县级市"){
		commonTckBlack("请将您地址的省市区填写完整");
	   return;
		}
	var time="";
     time=$('#time option:selected').val();
     var clock=$('#clock option:selected').val();
       if(time=="请选择服务日期"){
		commonTckBlack("请选择服务日期");
	   return;
	   } 
	   if($(".clockSpan").html()=="请选择服务时间"){
		commonTckBlack("请选择服务时间");
	   return;
	   }
	  time=time.slice(0,11)+" "+$('.clockSpan').html();
   var personCount=$(".buyNumber").val();
   var allPrice=$(".lastPrice b").html();
   var remark=$("#remarkCon").html();
	   if(remark==null){
	   remark="";
	   }
   var partten = /^1[3,5,7,8]\d{9}$/;
	  if(isNull(name)){
	   commonTckBlack("请填写您的姓名");
	   return;
	    }
      if(partten.test(tel2))
          { }
		else
          {
      commonTckBlack("请填写正确的联系电话");
	   return;
          } 
	  if(isNull(address)){
	   commonTckBlack("请填写您的地址");
	   return;
	  }
	  if(isNull(address2)){
	   commonTckBlack("请填写您的详细地址");
	   return;
	  } 

	$.post(initData.checkCodeApi,{"phoneNo":tel1,"checkCode":code,"deviceId":"w_"+initData.userOpenId},function(data){
	  if(Number(data.success==1)){
	   var applyData={
	        "userId":data.data.userId,
			"token":data.data.token,
			"refreshToken":data.data.refreshToken,
			"address":address,
			"cookName":initData.cookName,
			"cardPrice":initData.cardPrice,
			"cardCode":initData.cardCode,
			"tel":tel1,
			"contactTel":tel2,
			"name":name,
			"time":time,
			"personCount":personCount,
			"totalAmount":allPrice,
			"remark":remark,
			}
		localStorage.applyData=JSON.stringify(applyData);
		
		window.location.href=initData.payUrl;}
		else{
		  commonTckBlack(data.errorMessage);
		}
	  })
	
})

//弹出黑框提示，1.5秒后自动关闭
   function commonTckBlack(txt){
	var td='<div class="common_tck_black">';
	td=td+txt;
	td=td+'</div>';
    var node2=document.createElement("div");
        node2.setAttribute("id","common_tck");	 
		document.body.appendChild(node2); 
		document.getElementById("common_tck").innerHTML=td;	  
	    setTimeout(function(){document.body.removeChild(node2)},1500);
	}	
//判断是否空格
   function isNull( str ){
    if ( str == "" ) return true;
    var regu = "^[ ]+$";
    var re = new RegExp(regu);
    return re.test(str);
}
//输入框获取焦点自动弹出到可视区域（防止输入法框遮盖输入框）
$("input").focus(function(){
	var scollTop=$(this).offset().top;
	setTimeout(function(){window.scrollTo(0,scollTop-100);
	},200)
})
$("textarea").focus(function(){
	var scollTop=$(this).offset().top;
	setTimeout(function(){window.scrollTo(0,scollTop-100);
	},200)
})

//禁用换行


function checkEnter(e)
{
var et=e||window.event;
var keycode=et.charCode||et.keyCode;
if(keycode==13)
{
if(window.event)
window.event.returnValue = false;
else
e.preventDefault();//for firefox
}

}